/**
 * @author: yangwk
 * @date: 2023/7/28
 * @description:
 **/
public class 最大子串和 {
    public static void main(String[] args) {
        System.out.println(maxSum(new int[]{ 3, 5, -2, 4, -1}));
        System.out.println(maxSumArr(new int[]{3, 5, -2, 4, -1}));
    }

    public static int maxSumArr(int[] arr) {
        int sum = 0;
        int max = 0;
        int right = 0, left = 0;
        while (right < arr.length) {
            sum += arr[right];
            while (left <= right && sum <= 0) {
                sum -= arr[left];
                left++;

            }
            max = Math.max(sum, max);
            right++;

        }
        return max;
    }

    public static int maxSum(int[] arr) {
        int sum = 0;
        int max = 0;
        for (int i : arr) {
            if (sum + i <= 0) {
                sum = 0;
            } else {
                sum += i;
            }
            max = Math.max(sum, max);

        }
        return max;
    }
}
